﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChangeAccountPopupPageEvent : EventManager {
    public override void InformationSetting()
    {

    }
    void LogoutBtnClick()
    {
        PlayerPrefs.Save();
        PlayerPrefs.SetString(DefaultKeyConst.VisitorTokenKey,"");
       // PlayerPrefs.DeleteAll();
        //Application.Quit();
       // return;
        SocketMgr.GetInstance().CloseSocket();
        GameObject.Find("StaticSource").GetComponent<WorldTime>().isConnecting = true;
        GameObject.Find("StaticSource").GetComponent<WorldTime>().Stoptimeout();    //开始发送心跳包
        Debug.Log("LogoutBtn");
        StartCoroutine("WaitSocketClose");
    }
    IEnumerator WaitSocketClose()
    {
        yield return new WaitForSeconds(0.05f);
 
            GameObject addnode =  GameObject.Find("addNode");
            DestroyChildren(addnode.transform);
            //弹出登陆界面
            UIManager.ChangeUI(UIManager.PageState.LoginPage, (GameObject obj) =>
            {
                SocketMgr.GetInstance().isNetOpen = false;
                obj.GetComponent<LoginPage>().InformationSetting();
            });
        
    }
    void DestroyChildren(Transform t)
    {
        bool isPlaying = Application.isPlaying;
        while (t.childCount != 0)
        {
            Transform child = t.GetChild(0);

            if (isPlaying)
            {
                child.parent = null;
                UnityEngine.Object.Destroy(child.gameObject);
            }
            else UnityEngine.Object.DestroyImmediate(child.gameObject);
        }
    }
    void close() {
        DestroyImmediate(this.gameObject);
    }
}
